const path = require('path')

const resolve = dir => {
    return path.join(__dirname, dir)
}

// 项目部署基础
// 默认情况下，我们假设你的应用将被部署在域的根目录下,
// 例如：https://www.my-app.com/
// 默认：'/'
// 如果您的应用程序部署在子路径中，则需要在这指定子路径
// 例如：https://www.foobar.com/my-app/
// 需要将它改为'/my-app/'
// iview-admin线上演示打包路径： https://file.iviewui.com/admin-dist/
const BASE_URL = process.env.NODE_ENV === 'production'
    ? '/'
    : '/'
const CompressionPlugin = require('compression-webpack-plugin')
module.exports = {
    // Project deployment base
    // By default we assume your app will be deployed at the root of a domain,
    // e.g. https://www.my-app.com/
    // If your app is deployed at a sub-path, you will need to specify that
    // sub-path here. For example, if your app is deployed at
    // https://www.foobar.com/my-app/
    // then change this to '/my-app/'
    configureWebpack: config => {
        // cdn引入
        config['externals'] = {
            'vue': 'Vue',
            'vue-router': 'VueRouter',
            'vuex': 'Vuex',
            'axios': 'axios',
            'view-design': 'iview',
            'vue-baidu-map': 'BaiduMap',
            '@babel/polyfill': '@babel/polyfill',
            'es6-promise': 'ES6Promise'
        }
        // gzip压缩
        if (process.env.NODE_ENV === 'production') {
            const terserWebpackPlugin = config.optimization.minimizer[0]
            const { terserOptions } = terserWebpackPlugin.options
            terserOptions.compress.drop_console = true
            terserOptions.compress.drop_debugger = true
            return {
                plugins: [new CompressionPlugin({
                    test: /\.js$|\.html$|\.css/,
                    threshold: 10240,
                    deleteOriginalAssets: false
                })

                ]
            }
        }
    },
    publicPath: BASE_URL,
    // tweak internal webpack configuration.
    // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
    // 如果你不需要使用eslint，把lintOnSave设为false即可
    lintOnSave: false,
    chainWebpack: config => {
        config.resolve.alias
            .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
            .set('_c', resolve('src/components'))
    },
    // 设为false打包时不生成.map文件
    productionSourceMap: false,
    // 这里写你调用接口的基础路径，来解决跨域，如果设置了代理，那你本地开发环境的axios的baseUrl要写为 '' ，即空字符串
    devServer: {
        // host: 'security.hf.com',
        proxy: {
            '/api': {
                // target: 'http://192.168.0.32:8101',
                // target: 'http://192.168.0.25:8101',
                // target: 'http://127.0.0.1:8101',
                target: 'http://admin.ck9696.com',
                // target: 'http://192.168.0.107:8009',//定时任务
                // target: 'http://wx.cunkou.co',
                // target: 'http://192.168.0.98:8101',
                // target: 'http://192.168.0.29',
                pathRewrite: {
                    '^/api': '/api'
                }
            },
            '^/oss/ckOSSServer/getCkOSSToken': {
                target: 'http://sts.ck9696.com/',
                pathRewrite: {
                               
                }
            }
        },
        open: true,
        port: 80 // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    }
}
